import { REQUEST_URL, FIELD, CARD_ADD_DISABLED_BUTTON, CARD} from "../../constant";
import { ajax, cardCache, promptBox } from 'nc-lightapp-front';
import { calcTotal } from "../../../../common/util/calcUtils";
let { setDefData} = cardCache;
/**
* @description: 表头编辑后事件
* @param: moduleId 区域编码
* @param: key 当前字段编码
*/
export function afterHeadEvent(props, moduleId, key, value, oldValue) {

        let headfields = null;
        let head_keys = ['tax'];
        let body_keys = ['money'];

        switch (key) {
                case FIELD.org://组织

                if(oldValue.value != ''){
                        promptBox({
                                color: 'warning',
                                title: "组织变更",
                                content: '更换组织将会清空界面信息',
                                noFooter: false,
                                noCancelBtn: true,
                                beSureBtnClick: ()=>{
                                        headfields = ['dept','accbank','def4','def5','','vouchertype','def12','def1','def2','def3','def9','def14','def15','def16','def18','ass','def21','def22'];
                                        clearSomeFields(props, headfields);
        
                                        value = calcTotal(props, CARD.form_id, head_keys, CARD.table_code, body_keys);
                                        props.form.setFormItemsValue(CARD.form_id, { 'count': { value: value} });
                                }
                        })
                }
                




                        props.resMetaAfterPkorgEdit(); //选择主组织以后，恢复其他字段的编辑性
                        props.button.setButtonDisabled(CARD_ADD_DISABLED_BUTTON, false);//恢复增行编辑性
                        props.cardTable.setAllCheckboxAble(CARD.table_code, true);
                        //设置核算账簿缓存
                        filterByOrg(props, moduleId);
                        javaAfterHeadEvent(props,key);
                        break;

                case 'def7':
                        if(oldValue.value != ''){
                                promptBox({
                                        color: 'warning',
                                        title: "利润中心变更",
                                        content: '更换将会清空表体信息',
                                        noFooter: false,
                                        noCancelBtn: true,
                                        beSureBtnClick: ()=>{
                                                clearSomeFields(props, headfields);
                
                                                value = calcTotal(props, CARD.form_id, head_keys, CARD.table_code, body_keys);
                                                props.form.setFormItemsValue(CARD.form_id, { 'count': { value: value} });
                                        }
                                })
                        }
                        

                        break;

                case 'ass'://贷方科目
                        javaAfterHeadEvent(props,key);
                        setAssRequiredFiled(props, key);
                        break;
        
                case 'dept'://编辑录单利润中心
                        //编辑录单利润中心置空报销部门
                        javaAfterHeadEvent(props,key);
                        break;
                        
                case 'accbank'://银行账户控制录单利润中心是否可编辑
                        if(value.value == ''){
                                props.form.setFormItemsDisabled(moduleId, { dept: false });
                        }else{
                                props.form.setFormItemsDisabled(moduleId, { dept: true });
                        }

                        javaAfterHeadEvent(props,key);
                        break;

                case 'tax'://增值税金额
                        setAssRequiredFiled(props, key);
                        value = calcTotal(props, CARD.form_id, head_keys, CARD.table_code, body_keys);
                        props.form.setFormItemsValue(CARD.form_id, { 'count': { value: value} });
                        break;

                default:
                        break;
        }
}

export function filterByOrg(props, moduleId) {
        let data = null;
        
        let value = props.form.getFormItemsValue(moduleId, 'pk_org');
        if (value.value != '') {
                data = value;
        }

        if (data != null) {
                ajax({
                        url: REQUEST_URL.getOrgMap,
                        data: data,
                        success: (res) => {
                                if (res.data.accbook != null) {
                                        setDefData("accbook", "accbook", res.data.accbook);//表体科目过滤使用
                                }
                        }
                });
        }
}

export function javaAfterHeadEvent(props, key){

        let bill = props.createMasterChildData(CARD.page_code, CARD.form_id, CARD.table_code, 'cardTable');

        let data = {
                key : key,
                bill : bill
        }

        ajax({
                url : REQUEST_URL.afterHeadEvent,
                data : data,
                success: (res) => {
                        if(res.data && res.data.head){
                                //表头
                                props.form.setAllFormValue({ [CARD.form_id]: res.data.head[CARD.form_id] });
                        }
                        // if (res.data && res.data.body) {
                        //         //表体
                        //         props.cardTable.setTableData(CARD.table_code, res.data.body[CARD.table_code]);
                        // }
                }
                
        })
}

export function setAssRequiredFiled(props, key){

        let bill = props.createMasterChildData(CARD.page_code, CARD.form_id, CARD.table_code, 'cardTable');

        let data = {
                key : key,
                bill : bill
        }

        ajax({
                url : REQUEST_URL.afterHeadEditField,
                data : data,
                success: (res) => {
                        props.form.setFormItemsRequired(CARD.form_id, res.data);

                }
        })
}

function clearSomeFields(props, headfields){

        // if(bodyfields != null){
        //     for(let i = 0; i < bodyfields.length; i++) {
        //         props.cardTable.setValByKeyAndIndex(CARD.table_code,index,bodyfields[i],{ value : "", display : ""});
        //     }
        // }
        let count = props.cardTable.getAllRows(CARD.table_code).length;
        for(let i = 0; i < count; i ++){
                props.cardTable.delRowsByIndex(CARD.table_code, [0]);
        }
        props.cardTable.setTableData(CARD.table_code, []);
        
        if(headfields != null){
            for(let i = 0; i < headfields.length; i++) {
                let value = new Object;
                value[headfields[i]] = { value : "", display : ""};
                props.form.setFormItemsValue(CARD.form_id, value);
            }
        }
    }